/**   
 * Copyright © 2021 郑心怡. All rights reserved.
 * 
 * @Title:RoleServiceImpl.java 
 * @Prject: com.interesting.examsystem.role.service.impl
 * @Package: com.interesting.examsystem.role.service.impl
 * @author: ZhengXinyi   
 * @date: 2021-07-25
 * @version: V1.0   
 */
package com.interesting.examsystem.role.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.interesting.examsystem.common.utils.BeanMapperUtils;
import com.interesting.examsystem.role.model.RoleDO;
import com.interesting.examsystem.role.mapper.RoleMapper;
import com.interesting.examsystem.role.service.IRoleService;
import com.interesting.examsystem.role.model.RoleDTO;
import com.interesting.examsystem.role.model.RoleVO;

/**
 * @ClassName: RoleServiceImpl
 * @Description:  服务接口实现
 * @author: ZhengXinyi
 * @date: 2021-07-25
 */
@Slf4j
@Transactional
@Service("roleService")
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public Page<RoleVO> selectPageVo(Page<RoleDTO> page, RoleDTO roleDto) {
		OrderItem orderItem;
		if (!StrUtil.isEmpty(roleDto.getSortMapJson())) {
			JSONObject jsonObject = JSONUtil.parseObj(roleDto.getSortMapJson());
			for (Entry<String, Object> orderEntry : jsonObject.entrySet()) {
				orderItem = new OrderItem();
				orderItem.setAsc((boolean)orderEntry.getValue());
				orderItem.setColumn(orderEntry.getKey());
				page.addOrder(orderItem);
			}
		} else {
			orderItem = new OrderItem();
			orderItem.setAsc(true);
			orderItem.setColumn("SORT_NUMBER");
			page.addOrder(orderItem);
		}
		Page<RoleVO> resultPage = roleMapper.selectPageVo(page, roleDto);
		return resultPage;
	}
	
	@Override
	public RoleVO selectById(String id) {
		RoleVO roleVo = null;
		RoleDO role = (RoleDO) roleMapper.selectById(id);
		if (ObjectUtil.isNotEmpty(role)) {
			roleVo = BeanMapperUtils.map(role, RoleVO.class);
		}
		return roleVo;
	}
	
	@Override
	public RoleVO selectOne(RoleDTO roleDto) {
		RoleVO roleVo = null;
		RoleDO entity = BeanMapperUtils.map(roleDto, RoleDO.class);
		QueryWrapper<RoleDO> queryWrapper = new QueryWrapper<>();
		queryWrapper.setEntity(entity);
		RoleDO roleDo = roleMapper.selectOne(queryWrapper);
		if (ObjectUtil.isNotEmpty(roleDo)) {
			roleVo = BeanMapperUtils.map(roleDo, RoleVO.class);
		}
		return roleVo;
	}
	
	@Override
	public int deleteBatchIds(List<String> ids) {
		return roleMapper.deleteBatchIds(ids);
	}
	
	@Override
	public int insert(RoleVO roleVo) {
		roleVo.setId(IdUtil.objectId());
		RoleDO entity = BeanMapperUtils.map(roleVo, RoleDO.class);
		return roleMapper.insert(entity);
	}
	
	@Override
	public int update(RoleVO roleVo) {
		UpdateWrapper<RoleDO> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", roleVo.getId());
		RoleDO entity = BeanMapperUtils.map(roleVo, RoleDO.class);
		return roleMapper.update(entity, updateWrapper);
	}
	
	@Override
	public void insertBatch(List<RoleVO> roleVos) {
		if (!ObjectUtil.isEmpty(roleVos)) {
			roleMapper.insertBatch(roleVos);
		}
	}
}
